package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.RoleService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 包名:  com.iteitan.service.impl;
 * 作者:  Eitan
 * 日期:  2019/9/9 18:43
 * 角色管理的服务实现类
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 添加角色以及设置权限,设置菜单
     * @param map
     */
    @Override
    public void add(Map map) {
        List<Integer> permissionIds = (List<Integer>) map.get("permissionIds");
        List<Integer> menuIds = (List<Integer>) map.get("menuIds");
        Map<Object,Object> roleMap= (Map<Object, Object>) map.get("formData");
        Role role = new Role();
        role.setName((String) roleMap.get("name"));
        role.setKeyword((String) roleMap.get("keyword"));
        role.setDescription((String) roleMap.get("description"));

        roleDao.add(role);
        if(permissionIds.size()>0 && permissionIds != null){
            setRoleAndPermission(role.getId(),permissionIds);
        }
        if(menuIds.size()>0 && menuIds !=null){
            setRoleAndMenu(role.getId(),menuIds);
        }
    }

    /**
     * 设置角色的权限,绑定与权限表的关系
     * @param roleId
     * @param permissionIds
     */
    private void setRoleAndPermission(Integer roleId,List<Integer> permissionIds){
        for (Integer permissionId : permissionIds) {
            Map<String,Object> map = new HashMap<>();
            map.put("role_id",roleId);
            map.put("permission_id",permissionId);
            roleDao.setRoleAndPermission(map);
        }
    }

    /**
     * 设置角色访问菜单,绑定菜单与角色的关系
     * @param roleId
     * @param menuIds
     */
    private void setRoleAndMenu(Integer roleId,List<Integer> menuIds){
        for (Integer menuId : menuIds) {
            Map<String,Object> map = new HashMap<>();
            map.put("role_id",roleId);
            map.put("menu_id",menuId);
            roleDao.setRoleAndMenu(map);
        }
    }

    /**
     * 角色分页
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page= roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public void deleteById(Integer id) throws Exception {

        int menuRole=roleDao.findRoleMenuById(id);
        int authRole=roleDao.findRoleAuthById(id);
        int userRole=roleDao.findRoleUserById(id);
        if(menuRole>0 || authRole>0 || userRole> 0){
            throw new RuntimeException("存在其他数据关联,无效操作!");
        }
        roleDao.deleteById(id);
    }

    /**
     * 查询所有的权限
     * @return
     */
    @Override
    public List<Permission> findPermissionAll() {
        return permissionDao.findPermissionAll();
    }

    /**
     * 根据角色id找角色权限
     * @param roleId
     * @return
     */
    @Override
    public Set<Permission> findPermissionByRoleId(String roleId) {
        return permissionDao.findPermissionByRowId(Integer.parseInt(roleId));
    }

    /**
     * 根据角色id找角色
     * @param id
     * @return
     */
    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    /**
     * 修改角色数据
     * @param map
     */
    @Override
    public void edit(Map map) {
        List<Integer> permissionIds = (List<Integer>) map.get("permissionIds");
        List<Integer> menuIds = (List<Integer>) map.get("menuIds");
        Map<Object,Object> roleMap= (Map<Object, Object>) map.get("formData");
        Role role = new Role();
        role.setId((Integer) roleMap.get("id"));
        role.setName((String) roleMap.get("name"));
        role.setKeyword((String) roleMap.get("keyword"));
        role.setDescription((String) roleMap.get("description"));

        roleDao.updateRole(role);
        //1.删除更新第三张表的数据
        Integer roleId = role.getId();
        if(permissionIds.size()>0 && permissionIds != null){
            roleDao.deleteRolePermissionByRoleid(roleId);
            //2.重新建立与第三张表关系
            setRoleAndPermission(roleId,permissionIds);
        }
        if(menuIds.size()>0 && menuIds != null){
            //1.删除更新第三张表的数据
            roleDao.deleteRoleMenuByRoleid(roleId);
            //2.重新建立与第三张表关系
            setRoleAndMenu(role.getId(),menuIds);
        }

    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<Role> findRoleAll() {
        return roleDao.findRoleAll();
    }


}
